import network
import socket
import time
import ntptime

SSID = "HelloWorld"  #修改为你的WiFi名称
PASSWORD = "456258000"  #修改为你WiFi密码
port = 10000  #端口号
wlan = None  #wlan
listenSocket = None  #套接字


# 连接WiFi
def connectWifi():
  global SSID, PASSWORD
  wlan = network.WLAN(network.STA_IF) 
  wlan.active(True)   #激活网络
  wlan.disconnect()   #断开WiFi连接
  wlan.connect(SSID, PASSWORD)   #连接WiFi
  connectCounter = 0
  while connectCounter < 5:
    if not wlan.isconnected():   #等待连接
      time.sleep(1)
      connectCounter += 1
    else:
      break
  if wlan.isconnected():
    return wlan
  return None

def connectServer():
  pass


# 时间校准函数
def sync_ntp():
     ntptime.NTP_DELTA = 3155644800   # 可选 UTC+8偏移时间（秒），不设置就是UTC0
     ntptime.host = 'ntp1.aliyun.com'  # 可选，ntp服务器，默认是"pool.ntp.org"
     ntptime.settime()   # 修改设备时间,到这就已经设置好了

#Catch exceptions,stop program if interrupted accidentally in the 'try'
# connectWifi(SSID,PASSWORD)
# ip = wlan.ifconfig()[0]   #获取IP地址
# print(ip)
# while True:
#     pass
# wlan.disconnect()   #断开WiFi
wlan = connectWifi()
print(wlan.ifconfig()[0])
# response, timp, avg1 = req.get('https://www.jhxblog.cn/message/getmessagepagenum')
# print(response)


# try:
#     import urequests as requests
# except ImportError:
#     import requests

# r = requests.get("http://www.rt-thread.com/service/rt-thread.txt")
# print(r)
# r.close()
